<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/14 0014
 * Time: 14:56
 */

namespace app\admin\controller;


use app\admin\model\Notice;
use think\Controller;
use think\Db;
use think\Validate;
class AdminNotice extends Controller
{
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
    //管理员上传公告
    public function notice_add(){
        if ($this->request->isPost()){
            $data=input('post.');
            $rule=[
                'title|标题'=>'require|max:25',
                'content|内容'=>'require'
            ];
            $validate=new Validate();
            $va=$validate->check($data,$rule);
            if ($va==true) {
                $add=Notice::create([
                    'title'=>$data['title'],
                    'content'=>$data['content'],
                    'author_name'=>$data['author']
                    ]);
                    if ($add){
                        $msg=$this->msg(200,'提交成功');
                    }else{
                        $msg=$this->msg(400,'提交失败');
                    }
            }else{
                    $msg=$this->msg(400,$validate->getError());
            }
            
            return $msg;
        }
        return $this->fetch();
    }
    //公告列表
    public function notice_list(){
        $notice=new Notice();
        $title=input('get.title');
        if (isset($title)) {
             $notices=$this->notice_check($title);
        }else{
             $notices=$notice->order('create_time','desc')->paginate(10);            
        }
        $this->assign('notices',$notices);
        return $this->fetch();
    }
    //公告删除
    public function notice_dele(){
        $id=input('post.id');
        $notice=Notice::destroy($id);
        if ($notice) {
            $msg=$this->msg(200,'删除公告成功');
        }else{
            $msg=$this->msg(400,'删除公告失败');
        }
        return $msg;
    }
    //公告多项删除
    public function notice_alldele(){
        $id=input('post.id');
        $notice=Notice::destroy($id);
        if ($notice) {
            $msg=$this->msg(200,'删除所选公告成功');
        }else{
            $msg=$this->msg(400,'删除所选公告失败');
        }
        return $msg;
    }
    //公告检索
    public function notice_check($title){
        $notice=new Notice();
        $notices=$notice->where('title','like','%'.$title.'%')->order('create_time','desc')->paginate(10);
        return $notices;
    }
    //公告更新
    public function notice_update($id){
        $notice=new Notice();
        $notice=$notice->where('id',$id)->find();
        if ($this->request->isPost()) {
            $data=input('post.');
            $update=$notice->where('id',$id)->update([
                'title'  =>$data['title'],
                'content'=>$data['content']
            ]);
            if ($update) {
                $msg=$this->msg(200,'更新成功');
            }else{
                $msg=$this->msg(400,'更新失败');
            }
            return $msg;
        }
        $this->assign('notice',$notice);
        return $this->fetch();
    }
    //公告图片上传
    public function notice_upload(){
         // 获取表单上传文件 例如上传了001.jpg
         $file = request()->file('images');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( '../public/uploads');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            $msg=[
                'errno'=>0,
                'data' =>['/uploads/'.$info->getSaveName()]
            ];
        }else{
            // 上传失败获取错误信息
            $msg=[
                'errno'=>1
            ];
            }
            return json($msg);
    }
    //数据返回
    private function msg($statu,$msg){
        return [
            'statu'=>$statu,
            'msg'  =>$msg
        ];
    }
}